package com.ay.smartpark.sdk.common.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

public class DateUtils {

    public final static String TIME = "yyyy-MM-dd HH:mm:ss";
    public final static String YEAR = "yyyy";
    public final static String DAY = "yyyy-MM-dd";
    public final static String DAYS = "yyyyMMdd";
    public final static SimpleDateFormat SDF_YEAR = new SimpleDateFormat(YEAR);
    public final static SimpleDateFormat SDF_DAY = new SimpleDateFormat(DAY);
    public final static SimpleDateFormat SDF_DAYS = new SimpleDateFormat(DAYS);
    public final static SimpleDateFormat SDF_TIME = new SimpleDateFormat(TIME);

    private static final Logger logger = LoggerFactory.getLogger(DateUtils.class);


    /**
     * @Description: 获取YYYY-MM-DD hh:mm:ss格式
     * @Param: []
     * @return: java.lang.String
     * @Author: ChengQi
     * @Date: 2019/5/7
     */
    public static String getTime() {
        return SDF_TIME.format(new Date());
    }

    /**
     * @Description: 获取当月开始时间
     * @Param: [formatValue]
     * @return: java.lang.String
     * @Author: ChengQi
     * @Date: 2019/7/14
     */
    public static String getCurrentMonthStartTime(String formatValue) {
        SimpleDateFormat format = new SimpleDateFormat(formatValue);
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.MONTH, 0);
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MINUTE, 0);
        return format.format(calendar.getTime());
    }

    /**
     * @Description: 获取当月结束时间
     * @Param: [formatValue]
     * @return: java.lang.String
     * @Author: ChengQi
     * @Date: 2019/7/14
     */
    public static String getCurrentMonthEndTime(String formatValue) {
        SimpleDateFormat format = new SimpleDateFormat(formatValue);
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.MONTH, 0);
        calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.SECOND, 59);
        calendar.set(Calendar.MINUTE, 59);
        return format.format(calendar.getTime());
    }

    public static Date getDateFromDateJudice(Date date, Integer length, int type) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(type, calendar.get(type) + length);
        return calendar.getTime();
    }

    /**
     * 获取指定日期前length月份集合
     *
     * @param dateKey 当前时间月份
     * @param length  月份，向前推几个月
     * @Author: DingYongJun
     * @return
     */
    public static String[] getDateListBeforeLength(String dateKey, Integer length) throws ParseException {
        String[] dateArr = new String[length];
        dateArr[0] = dateKey;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
        Date beginDate = sdf.parse(dateKey);
        for (Integer i = 1; i < length; i++) {
            Date endDate = getDateFromDateJudice(beginDate, 0 - i, Calendar.MONTH);
            String endKey = sdf.format(endDate);
            dateArr[i] = endKey;
        }
        return dateArr;
    }

    //获取半年的时间  即最开始月份,和结尾月份
    public static Map<String,Object> getTime(String dateKey, Integer length) throws ParseException {
        Map<String, Object> map = new HashMap<>();
        List listTime = new ArrayList();
        //获得往前推6个月的月份集合 以及按照时间排序
        listTime = DateUtils.compareBytime(Arrays.asList(DateUtils.getDateListBeforeLength(dateKey, length)));
            map.put("startTime", listTime.get(0));
            int index = listTime.size();
            map.put("endTime", listTime.get(index - 1));
        return map;
    }

    //传入集合,按照时间排序
    public static List compareBytime(List list) {
        Collections.sort(list, new Comparator<String>() {
            DateFormat f = new SimpleDateFormat("yyyy-MM");
            @Override
            public int compare(String o1, String o2) {
                try {
                    return f.parse(o1).compareTo(f.parse(o2));
                } catch (ParseException e) {
                    throw new IllegalArgumentException(e);
                }
            }
        });
        return list;
    }

    /**
     * 获取传入日期格式为2019-02 的日期的当月开始时间 (弱智版)
     *
     * @param
     * @param
     * @return
     */
    public static String getMonthStartTime(String formatValue) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        formatValue = formatValue.toString() + "-01 00:00:00";
        try {
            Date date = dateFormat.parse(formatValue); //传入的字符串转换日期
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);
            //设置日期为当月第一天
            calendar.set(Calendar.DAY_OF_MONTH, 1);
            calendar.set(Calendar.HOUR_OF_DAY, 0);
            calendar.set(Calendar.SECOND, 0);
            calendar.set(Calendar.MINUTE, 0);
            Date date1 = calendar.getTime();
            String resultDate = dateFormat.format(date1);
            return resultDate;
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return formatValue;
    }

    /**
     * 获取传入日期格式为2019-02 的日期的当月结束时间 (弱智版)
     *
     * @param
     * @param
     * @return
     */
    public static String getMonthEndTime(String formatValue) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        formatValue = formatValue.toString() + "-01 00:00:00";
        try {
            Date date = dateFormat.parse(formatValue); //传入的字符串转换日期
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);
            //设置日期为当月最后
            calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
            calendar.set(Calendar.HOUR_OF_DAY, 23);
            calendar.set(Calendar.SECOND, 59);
            calendar.set(Calendar.MINUTE, 59);
            Date date1 = calendar.getTime();
            String resultDate = dateFormat.format(date1);
            return resultDate;
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return formatValue;
    }

    /**
     * 获取指定日期前length月份集合
     *
     * @param dateKey 当前时间月份
     * @param length  月份，向前推几个月
     * @Author: DingYongJun
     * @return  返回当前月份和前推的月份
     */
    public static String[] getDateBeforeMonth(String dateKey, Integer length){
        String[] arrData = new String[2];
        try {
            //dateKey是传进来的日期，length是所需要前推的月份
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM");
            Calendar c = Calendar.getInstance();
            Date nowMonth = format.parse(dateKey);
            c.setTime(nowMonth);
            c.add(Calendar.MONTH, -length);
            Date m3 = c.getTime();
            String mon3 = format.format(m3);
            System.out.println("过去"+length+"个月："+mon3);
            arrData[0] = mon3;
            arrData[1] = dateKey;
            return arrData;
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return arrData;
    }


    public static String dateDiff(String startTime, String endTime, String format) throws ParseException {
        //按照传入的格式生成一个simpledateformate对象
        SimpleDateFormat sd = new SimpleDateFormat(format);
        long nd = 1000*24*60*60;//一天的毫秒数
        long nh = 1000*60*60;//一小时的毫秒数
        long nm = 1000*60;//一分钟的毫秒数
        long ns = 1000;//一秒钟的毫秒数
        long diff;
        //获得两个时间的毫秒时间差异
        diff = sd.parse(endTime).getTime() - sd.parse(startTime).getTime();
        long day = diff/nd;//计算差多少天
        long hour = diff%nd/nh;//计算差多少小时
        long min = diff%nd%nh/nm;//计算差多少分钟
        //long sec = diff%nd%nh%nm/ns;//计算差多少秒//输出结果
        StringBuffer stringBuffer = new StringBuffer();
        if(day>0){
            stringBuffer.append(day+"天");
        }
        if(hour>0){
            stringBuffer.append(hour+"小时");
        }
        if(min>0){
            stringBuffer.append(min+"分钟");
        }
        if(min<0){
            stringBuffer.append("<1分钟");
        }
        //System.out.println("时间相差："+day+"天"+hour+"小时"+min+"分钟");
        return stringBuffer.toString() ;
    }


}
